import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
var _excluded = ["current", "pageSize"];
import { useState } from 'react';
import useFetch from './useFetch';
import { defaultPagination } from '../consts';
var defaultTransform = function defaultTransform(data, pagination) {
  return Array.isArray(data) ? {
    pagination: {
      current: 1,
      pageSize: pagination ? pagination.pageSize : defaultPagination.pageSize,
      total: (data || []).length
    },
    list: data || []
  } : {
    pagination: {
      current: data.current,
      pageSize: data.pageSize,
      total: data.total
    },
    list: data.list || []
  };
};
var getDataSourceByPagination = function getDataSourceByPagination(list, pagination) {
  var _pagination$current = pagination.current,
    current = _pagination$current === void 0 ? 0 : _pagination$current,
    _pagination$pageSize = pagination.pageSize,
    pageSize = _pagination$pageSize === void 0 ? defaultPagination.pageSize : _pagination$pageSize,
    _pagination$total = pagination.total,
    total = _pagination$total === void 0 ? 0 : _pagination$total;
  return (list || []).slice(0 + pageSize * (current - 1), total < pageSize * current ? total : pageSize * current);
};
var useBaseTable = function useBaseTable(request) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var enableFakePagination = options.enableFakePagination,
    _options$transfrom = options.transfrom,
    _transfrom = _options$transfrom === void 0 ? defaultTransform : _options$transfrom;
  var _useState = useState(defaultPagination),
    _useState2 = _slicedToArray(_useState, 2),
    pagination = _useState2[0],
    setPagination = _useState2[1];
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    dataSource = _useState4[0],
    setDataSource = _useState4[1];
  var _useFetch = useFetch(request, _objectSpread(_objectSpread({}, options), {}, {
      // @ts-ignore
      transfrom: function transfrom(data) {
        var temp = _transfrom(data, pagination);
        setPagination(temp.pagination);
        var dataSource = enableFakePagination ? getDataSourceByPagination(temp.list, temp.pagination) : temp.list;
        setDataSource(dataSource);
        return temp;
      }
    })),
    params = _useFetch.params,
    data = _useFetch.data,
    loading = _useFetch.loading,
    search = _useFetch.search,
    research = _useFetch.research,
    reset = _useFetch.reset;
  var onChange = function onChange(pagination, filters, sorter) {
    var current = pagination.current,
      pageSize = pagination.pageSize;
    if (enableFakePagination) {
      var pag = {
        current: current,
        pageSize: pageSize,
        total: pagination.total
      };
      setPagination(pag);
      setDataSource(getDataSourceByPagination((data || {
        list: []
      }).list, pag));
      return;
    }
    // @ts-ignore
    research({
      current: current,
      pageSize: pageSize,
      sorter: sorter ? sorter : undefined,
      filters: filters ? filters : undefined
    });
  };
  var current = params.current,
    pageSize = params.pageSize,
    searchFields = _objectWithoutProperties(params, _excluded);
  return {
    params: params,
    searchFields: searchFields,
    props: {
      pagination: pagination,
      dataSource: dataSource,
      loading: loading,
      // @ts-ignore
      onChange: onChange
    },
    search: search,
    research: research,
    reset: reset
  };
};
export default useBaseTable;